package com.ph.seata.storage.controller;


import com.ph.seata.storage.dto.CommodityDTO;
import com.ph.seata.storage.model.StorageDO;
import com.ph.seata.storage.service.StorageService;
import com.ph.web.common.response.WrapperResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author swallow
 * @since 2021-06-29
 */
@RestController
@RequestMapping("/api/storage")
@Slf4j
@RequiredArgsConstructor
public class StorageController {

    private final StorageService storageService;

    @PostMapping(value = "/dec_storage")
    public WrapperResponse<Object> decreaseStorage(@RequestBody CommodityDTO commodityDTO) {
        log.info("请求扣减库存:{}", commodityDTO.toString());
        return storageService.decreaseStorage(commodityDTO);
    }

    @GetMapping(value = "/load_object")
    public WrapperResponse<StorageDO> loadObject(CommodityDTO commodityDTO) {
        return storageService.loadObject(commodityDTO);
    }

    @GetMapping(value = "/load_object_globalLock")
    public WrapperResponse<StorageDO> loadObjectAndGlobalLock(CommodityDTO commodityDTO) {
        return storageService.loadObjectAndGlobalLock(commodityDTO);
    }

    @GetMapping(value = "/load_object_transactional")
    public WrapperResponse<StorageDO> loadObjectAndGlobalTransactional(CommodityDTO commodityDTO) {
        return storageService.loadObjectAndGlobalTransactional(commodityDTO);
    }

    @GetMapping(value = "/load_object_for_update")
    public WrapperResponse<StorageDO> loadObjectForUpdate(CommodityDTO commodityDTO) {
        return storageService.loadObjectForUpdate(commodityDTO);
    }

    @GetMapping(value = "/load_object_globalLock_for_update")
    public WrapperResponse<StorageDO> loadObjectAndGlobalLockForUpdate(CommodityDTO commodityDTO) {
        return storageService.loadObjectAndGlobalLockForUpdate(commodityDTO);
    }

    @GetMapping(value = "/load_object_transactional_for_update")
    public WrapperResponse<StorageDO> loadObjectAndGlobalTransactionalForUpdate(CommodityDTO commodityDTO) {
        return storageService.loadObjectAndGlobalTransactionalForUpdate(commodityDTO);
    }

}

